//
//  ZWHRegisterViewController.swift
//  茶吧
//
//  Created by Syrena on 2017/7/5.
//  Copyright © 2017年 Syrena. All rights reserved.
//

import UIKit
import Hyphenate
import AVOSCloud

class ZWHRegisterViewController: UIViewController {
    @IBOutlet weak var phone: UITextField!
    
    @IBOutlet weak var passwordF: UITextField!
    
    @IBOutlet weak var requestNumF: UITextField!
    
    @IBOutlet weak var requestB: UIButton!
    
    @IBOutlet weak var registerB: UIButton!
    
    
    override func viewWillAppear(_ animated: Bool) {
        navigationController?.isNavigationBarHidden = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setnavigation(title: "注册", titlesize: 13, titlecolor: #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), backimage: "黑色返回", bartintcolor: #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1))
        phone.delegate = self
        passwordF.delegate = self
        requestNumF.delegate = self
        passwordF.isSecureTextEntry = true
        registerB.isEnabled = false
        requestB.isEnabled = false
        requestNumF.keyboardType = .numberPad
        
    }
    
    

    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
    
    //请求验证码
    @IBAction func requestNumClicked(_ sender: UIButton) {
        let user = AVUser()
        user.username = phone.text
        user.password = passwordF.text
        user.mobilePhoneNumber = phone.text
        user.setObject(phone.text, forKey: "name")
        
        user.signUpInBackground { [weak self](success, error) in
            if success == true{
                ZWHAlertController.showAlert(vc: self!, title: "发送成功", message: "", style:.alert)
            }else{
                AVUser.requestMobilePhoneVerify((self?.phone.text!)!, with: { (success, error) in
                    if success == true{
                        ZWHAlertController.showAlert(vc: self!, title: "发送成功", message: "", style:.alert)
                    }else{
                        ZWHAlertController.showAlert(vc: self!, title: "发送失败", message: "", style:.alert)
                    }
                })
            }
        }
        
            }

    
    //注册
    @IBAction func registerClicked(_ sender: UIButton) {
        
        AVUser.verifyMobilePhone(requestNumF.text!) { [weak self](success, error) in
            if success == true{
                EMClient.shared().register(withUsername: self?.phone.text, password: self?.passwordF.text) { (nil, error) in
                    if error == nil{
                        self?.navigationController?.popViewController(animated: true)
                                             
                    }else{
                        ZWHAlertController.showAlert(vc: self!, title: "注册失败", message: "", style: .alert)
                    }
                }
            }else{
                ZWHAlertController.showAlert(vc: self!, title: "验证码错误", message: "", style: .alert)
            }
        }

        
        
}
    
    //设置导航栏
    func setnavigation(title:String?,titlesize:CGFloat,titlecolor:UIColor,backimage:String?,bartintcolor:UIColor){
        
        navigationItem.title = title
        navigationController?.navigationBar.barTintColor = bartintcolor
        //定义标题颜色与字体大小字典
        let dict:NSDictionary = [NSForegroundColorAttributeName: titlecolor,NSFontAttributeName : UIFont.boldSystemFont(ofSize: titlesize)]
        //标题设置颜色与字体大小
        
        navigationController?.navigationBar.titleTextAttributes = dict as? [String : AnyObject]
        
        let Btn = UIButton(frame: CGRect(x: 0, y: 0, width: 15, height: 15))
        Btn.setImage(UIImage.init(named: backimage!), for: .normal)
        Btn.addTarget(self, action: #selector(backClicked), for: .touchUpInside)
        let back = UIBarButtonItem(customView: Btn)
        navigationItem.leftBarButtonItem = back
    }
    
    func backClicked() {
        navigationController?.popViewController(animated: true)
    }

}


extension ZWHRegisterViewController : UITextFieldDelegate{
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextFieldDidEndEditingReason) {
        if phone.text != "" && passwordF.text != "" && requestNumF.text != "" {
            registerB.isEnabled = true
        }else{
            registerB.isEnabled = false
        }
        
        if phone.text != "" && passwordF.text != "" {
            requestB.isEnabled = true
            requestB.setTitleColor(#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1), for: .normal)
        }else{
            requestB.isEnabled = false
            requestB.setTitleColor(#colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1), for: .normal)
        }
    }
    
}
